<?php
/**
 * Created by dzw.
 * User: Administrator
 * Date: 2018-03-16
 * Time: 10:22
 */

namespace App\Http\Controllers\Web\Member\Seller;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;

class ChangeController extends BaseController
{
    protected $api_url = [
        // 退单列表
        'index' => ['get', 'seller/change'],
        // 退单详情
        'show'  => ['get', 'seller/change/%d'],
        // 更新退单
        'update'=> ['put', 'seller/change/%d'],
        // 删除退单
        'delete'=> ['delete', 'seller/change/%d'],
    ];
    protected $api_host;

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_web');
    }

    /* 
     * 获取 seller 店铺的退货款列表
     */
    public function indexAction()
    {
        //参数部分
        $search_params = $this->request->all(['created_at','status']);
        if ($search_params['status']==2) {
            $search_params['status'] = array('1','2','3','4','5','6');
        }elseif ($search_params['status']==3){
            $search_params['status'] = array('0');
        }else{
            $search_params['status'] = array('0','1','2','3','4','5','6');
        }
        $page = $this->request->get('page', 1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'page' => $page,
            'with' => 'orderProducts'
        ];
        $status = $this->request->get('status', 0);

        //请求部分
        $ret = http_web($this->api_url['index'])->send($params);

        $search_params['status'] = $status;
        //回填搜索框值
        $ret['data']['search_params'] = $search_params;
        $ret['data']['status'] = $status;
        //结果处理部分 与 page 页面值不匹配
        $data = paginate($this->request, $ret['data']);

        //结果处理部分
        return view('web.user.seller.return_goods_list', $data);
        
    }
    /* 
     * 退货款单详情
     */
    public function showAction($id)
    {
        //请求部分
        $ret = http_web($this->api_url['show'],[$id])->send();

        //结果处理部分
        return view('web.user.seller.change_detail', $ret['data']);

    }
    /* 
     * 更新退货款单状态
     */
    public function storeAction()
    {        
        //请求部分
        $id = $this->request->post()['id'];
        $data['user_id'] = 2;
        $data['change_id'] = $id;
        // 获取ajax 传递的状态
        $data['status']  = $this->request->post()['status'];
        $data['shop_id'] = 2;
        
        $ret = http_web($this->api_url['update'],[$id])->send($data);

        //结果处理部分
        if(isset($ret['code']) && $ret['code']===0 ){
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>isset($ret['message']) ? $ret['message'] : $ret['data']]);
        }
        // return view('web.user.seller.change_list', $ret['data']);

    }
    /* 
     * 删除退货款单状态
     */
    public function deleteAction()
    {
        //请求部分
        $id = $this->request->delete()['id'];
        $ret = http_web($this->api_url['delete'],[$id])->send();

        //结果处理部分
        return view('web.user.buyer.repair', $ret['data']);

    }

    public function log($data,$filename){
        if(empty($filename)){
            $log_filename='C:\Users\admin\Desktop\ecclub.php';
        }
        else{
            $log_filename='C:\Users\admin\Desktop\\'.$filename.'.php';
        }
        // 如果文件夹不存在则创建文件夹
        is_dir(dirname($log_filename)) || mkdir(dirname($log_filename),0777,true);
        // 日志大小
        $max_size=10485760;
        // 判断文件大小做自动删除动作
        if(file_exists($log_filename) && abs(filesize($log_filename))>$max_size){
            // 删除文件
            unlink($log_filename);
        }else{
            // 写日志(第三个参数是系统函数，可以连续写文件不覆盖)
            file_put_contents($log_filename, date("Y-m-d H:i:s")." ".$data."\n", FILE_APPEND);
        }
    }

}